<!DOCTYPE html>
<html>

<head>
<title></title>
<meta http-equiv="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit">

<%include("/common/include.html"){}%>
<style type="text/css">
</style>
</head>

<body class="easyui-layout">
	<table id="addTargetTable" class="easyui-datagrid" style="height:100%;">
		
	</table>
	
	<form id="addTargetForm">
		<input id="customerIds" type="hidden" value="${customer.ids! }" name="delWeiTargetSum.customerIds">
		<div id="tb" style="height:auto">
		
			起始日期:<input id="startTime" 
						<%if(isEmpty(_startTime)){%>
							value="${customer.registtime! }" 
						<%}else{%>
							value="${_startTime! }" 
						<%}%>
					class="easyui-datebox theme-textbox-radius"  style="width:15%;">
			截止日期:<input id="endTime" class="easyui-datebox theme-textbox-radius" style="width:15%;" >
			目标模式:<#code id="codeIds" class="easyui-combobox theme-textbox-radius" code="code_mbms" style="width: 15%;"/>
			<br>
			当前体重:<input id="startWei"  value="${_nowWeight! }" class="easyui-textbox theme-textbox-radius" style="width:135px;"  data-options="label:'斤',precision:1" labelPosition="after" >
			<input id="targetWei" type="hidden">
			<input id="delWei" type="hidden" >
			<!-- 目标体重:<input id="targetWei" type="hidden" class="easyui-textbox theme-textbox-radius" style="width:135px;" data-options="label:'斤',precision:1" labelPosition="after" >
			应减体重:<input id="delWei" type="hidden" class="easyui-textbox theme-textbox-radius" style="width:135px;"   data-options="label:'斤',precision:1" labelPosition="after" > -->
		</div>
	</form>
	
	<script type="text/javascript">
		var $form = $('#addTargetForm');
	
		var submitNow = function($dialog, $grid, $tree, $pjq) {
			if (endEditing()){
				
				var rows = $('#addTargetTable').datagrid('getRows');
				
				var delWeiTargetSum = 0;
				for(var i=0;i<rows.length;i++){
					var schedule = rows[i].schedule;
					var startTime = rows[i].starttime;
					var endTime = rows[i].endtime;
					var planToStoreNum = rows[i].plantostorenum;
					var delWeiTarget = rows[i].delweitarget;
					if(delWeiTarget==""){
						delWeiTarget = 0;
					}
					if(planToStoreNum==""){
						planToStoreNum = 0;
					}
					
					delWeiTargetSum = parseFloat(delWeiTargetSum)+parseFloat(delWeiTarget);
					delWeiTargetSum = delWeiTargetSum.toFixed(1);
					$("#addTargetForm").append("<input type='hidden' name='delWeiTargetList["+i+"].schedule' value='"+schedule+"'/>");
					$("#addTargetForm").append("<input type='hidden' name='delWeiTargetList["+i+"].startTime' value='"+startTime+"'/>");
					$("#addTargetForm").append("<input type='hidden' name='delWeiTargetList["+i+"].endTime' value='"+endTime+"'/>");
					$("#addTargetForm").append("<input type='hidden' name='delWeiTargetList["+i+"].planToStoreNum' value='"+planToStoreNum+"'/>");
					$("#addTargetForm").append("<input type='hidden' name='delWeiTargetList["+i+"].delWeiTarget' value='"+delWeiTarget+"'/>");
				}
				
				
				var startTime = $("#startTime").val();
				var endTime = $("#endTime").val();
				var codeIds = $("#codeIds").val();
				var startWei = $("#startWei").val();
				var targetWei = parseFloat(startWei)-parseFloat(delWeiTargetSum);
				targetWei = targetWei.toFixed(1);
				$("#addTargetForm").append("<input type='hidden' name='delWeiTargetSum.startTime' value='"+startTime+"'/>");
				$("#addTargetForm").append("<input type='hidden' name='delWeiTargetSum.endTime' value='"+endTime+"'/>");
				$("#addTargetForm").append("<input type='hidden' name='delWeiTargetSum.codeIds' value='"+codeIds+"'/>");
				$("#addTargetForm").append("<input type='hidden' name='delWeiTargetSum.startWei' value='"+startWei+"'/>");
				$("#addTargetForm").append("<input type='hidden' name='delWeiTargetSum.targetWei' value='"+targetWei+"'/>");
				$("#addTargetForm").append("<input type='hidden' name='delWeiTargetSum.delWei' value='"+delWeiTargetSum+"'/>");
			}
			
			var url = "${cxt!}/bloseWeight/addTarget";
			$.post(url, sy.serializeObject($form), function(result) {
				if (result.code == '200') {
					$dialog.dialog('destroy');
					$.slideMsg("添加成功！", $pjq);
					$grid.datagrid('reload');
					
				} else {
					$.alert($pjq, result.msg);
				}
			}, 'json');
		};
		var submitForm = function($dialog, $grid, $tree, $pjq) {
			submitNow($dialog, $grid, $tree, $pjq);
			// 					$form.isValid(function(isValied){
			// 						if(isValied){
			// 							submitNow($dialog, $grid, $pjq);
			// 						}
			// 					});
		};
		(function($){
			
			/* var customerIds=$('#customerIds').val();
			if(customerIds!="")
			{
				var url="/bentryRegister/selCustomerInfo";
				var data={"customerIds":customerIds};
				var divid="userinfo_div";
				common_ajax.ajaxDiv(divid,url,data);
			} */
			initTargetTable($("#codeIds").val());
			$('#codeIds').combobox({
				onChange:function(){
					
					var moshi = $("#codeIds").val();
					if(moshi!="c4bb5ef9657748a59c9b2c1784f401d5"){
						initTargetTable($("#codeIds").val());
						}else{
							//$('#endTime').attr("disabled",false); 
						//document.getElementById("endTime").disabled="";
						initShortTargetTable();
					}
				}
			});
			var moshi = $("#codeIds").val();
			if(moshi!="c4bb5ef9657748a59c9b2c1784f401d5"){
			initEndTime();
			}
			$('#startTime').datebox({
				onSelect:function(){
					var moshi = $("#codeIds").val();
					if(moshi!="c4bb5ef9657748a59c9b2c1784f401d5"){
					var startTime = load($("#startTime").val(), 30);
					$("#endTime").datebox('setValue', startTime);
					initTargetTable($("#codeIds").val());
					}else{
						
						initShortTargetTable();
					}
				}
			});
			$('#endTime').datebox({
				onSelect:function(){
					var moshi = $("#codeIds").val();
					if(moshi!="c4bb5ef9657748a59c9b2c1784f401d5"){
					
					}else{
						
						initShortTargetTable();
					}
				}
			});
			
		})(jQuery);
		
		//初始化截至日期
		function initEndTime(){
			var startTime = load($("#startTime").val(), 30);
			$("#endTime").val(startTime);
		}
		
		//初始化目标table
		function initTargetTable(codeIds){
			editIndex = undefined;//初始化行编辑
			var data=[];
			if(codeIds != -1){
				//通过ids获取模式下排期数据
				var url='/bloseWeight/getSchedule';
				var moshi={"moshiIds":codeIds};
				var reData = common_ajax.ajaxFunc(url,moshi,"json");
				var registTime = $("#startTime").val();
				
				if(registTime != ""){	
					for(var j=0;j<reData.length;j++){	
						var arr = reData[j].name.split('-');
						var starattime=arr[0];
						var endtime=arr[1];
						data.push({"schedule":reData[j].name,"starttime":load(registTime,starattime),"endtime":load(registTime,endtime),"plantostorenum":"","delweitarget":""});
					}
				}else{
					for(var j=0;j<reData.length;j++){	
						data.push({"schedule":reData[j].name,"starttime":"","endtime":"","plantostorenum":"","delweitarget":""});
					}
				}
			}
			$('#addTargetTable').datagrid({
				cls: "theme-datagrid",
				toolbar: '#tb',
				singleSelect: true,
				rownumbers: true, //显示序号
				columns:[[
							{field:'schedule',title:'排期',width:100,align:'center'},
							{field:'starttime',title:'起始日期',width:100,align:'center'},
							{field:'endtime',title:'截止日期',width:100,align:'center'},
							{field:'plantostorenum',title:'计划到店次数',width:100,align:'center',editor:{type:'numberspinner',
								min:1,
								max:100,
								options:{
								min:1
								}
						
						}},
							{field:'delweitarget',title:'减重目标（斤）',width:100,align:'center',editor:{type:'numberspinner',
								min:0.1,
								max:100,
								options:{
							   	 min:0.1,
							   	increment:0.1,
							   	precision:1,
								 onChange : function(n){
									 sum(n);
								},

								}
						
						}}
					    ]],
				data: data,
				onLoadSuccess:function(){
		        	
				},
				onClickRow:function(index, rowData){
					if (editIndex != index){
						if (endEditing()){
							$('#addTargetTable').datagrid('selectRow', index)
									.datagrid('beginEdit', index);
							editIndex = index;
						} else {
							$('#addTargetTable').datagrid('selectRow', editIndex);
						}
					}
				}
			});
		}
		
		//初始化短期目标table
		function initShortTargetTable(){
			editIndex = undefined;//初始化行编辑
			var data=[];
			var registTime = $("#startTime").val();
			var endtime =  $("#endTime").val();
			 
			data.push({"schedule":"1","starttime":registTime,"endtime":endtime,"plantostorenum":"","delweitarget":""});
			
			$('#addTargetTable').datagrid({
				cls: "theme-datagrid",
				toolbar: '#tb',
				singleSelect: true,
				rownumbers: true, //显示序号
				columns:[[
							{field:'schedule',title:'排期',width:100,align:'center'},
							{field:'starttime',title:'起始日期',width:100,align:'center'},
							{field:'endtime',title:'截止日期',width:100,align:'center'},
							{field:'plantostorenum',title:'计划到店次数',width:100,align:'center',editor:{type:'numberspinner',
								min:1,
								max:100,
								options:{
								min:1
								
								}
						
						}},
							{field:'delweitarget',title:'减重目标（斤）',width:100,align:'center',editor:{type:'numberspinner',
								min:0.1,
								max:100,
								options:{
								min:0.1,
								increment:0.1,
								precision:1,
								 onChange : function(n){
									 yingjian(n);
								}
								}
						
						}}
					    ]],
				data: data,
				onLoadSuccess:function(){
		        	
				},
				
				onClickRow:function(index, rowData){
					if (editIndex != index){
						if (endEditing()){
							$('#addTargetTable').datagrid('selectRow', index)
									.datagrid('beginEdit', index);
							editIndex = index;
						} else {
							$('#addTargetTable').datagrid('selectRow', editIndex);
						}
					}
				}
			});
		}
		
		function  load(date,days){
				days = parseInt(days)-1;
				var dateObj = new Date(date);
				dateObj.setDate(dateObj.getDate()+days); 
				var month=dateObj.getMonth()+1; 
				var day = dateObj.getDate(); 
				if(month<10){ 
				month = "0"+month; 
				} 
				if(day<10){ 
				day = "0"+day; 
				} 
				var val = dateObj.getFullYear()+"-"+month+"-"+day; 
				return val;
		}
		
		//计算应减斤数
		function sum(n)
		{	
			//alert(n)
			var arr=$('#addTargetTable').datagrid('getChanges');
			//var arr1=$('#addTargetTable').datagrid('getRows');
			//alert("arr11111111:"+  JSON.stringify(arr))
			//alert("arr22222222:"+JSON.stringify(arr1))
			
			var startWei = $("#startWei").val();
			var sum = 0;
		 	for(var i=0;i<arr.length;i++){
				var delWeiTarget = arr[i].delweitarget;
				sum = parseFloat(sum)+parseFloat(delWeiTarget);
				sum = sum.toFixed(1);
			}  
			var targetWei = parseFloat(startWei)-parseFloat(sum);
			targetWei = targetWei.toFixed(1);
			$('#delWei').textbox('setValue',sum);
			$('#targetWei').textbox('setValue',targetWei);
			
		}
		//计算应减斤数
		function  yingjian(n)
		{	
			var arr=$('#addTargetTable').datagrid('getChanges');
			var startWei = $("#startWei").val();
			//alert(arr.length);
			var sum = n;
			
			var targetWei = parseFloat(startWei)-parseFloat(sum);
			targetWei = targetWei.toFixed(1);
			$('#delWei').textbox('setValue',sum);
			$('#targetWei').textbox('setValue',targetWei);
			
		}
		
	</script>
	<script type="text/javascript">
		var editIndex = undefined;
		function endEditing(){
			if (editIndex == undefined){return true}
			if ($('#addTargetTable').datagrid('validateRow', editIndex)){
				$('#addTargetTable').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
	</script>

</body>
</html>